package com.miyake.server;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.miyake.client.datamanage.Text;

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
public class DsText {
	@PrimaryKey 
	@Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)  
	private Key key;
	
	@Persistent
	private List<String> text = new ArrayList<String>();

	@Persistent
	private String nodeKey;
	
	@Persistent
	private Boolean bold;
	
	@Persistent
	private Integer alignment;
	
	@Persistent
	private Integer fontsize;
	
	public DsText() {
	}
	
	public DsText(Text text2) {
		if (text2.getText() != null) {
			setText(text2.getText());
		}
		if (text2.getBold() != null) {
			this.setBold(text2.getBold());
		}
		if (text2.getAlign() != null) {
			this.setAlignment(this.getAlignment());
		}
		if (text2.getFontsize() != null) {
			this.setFontsize(text2.getFontsize());
		}
	}

	public String getText() {
		String ret = "";
		for (String s : text) {
			ret += s;
		}
		return ret;
	}
	
	public void setText(String text) {
		this.text.clear();
		int count = text.length() / 500;
		
		if (count >= 1) {
			for (int i = 0; i <= count; i++) {
				int end = i*500+499;
				if (end > text.length() - 1) {
					end = text.length() - 1;
				}
				this.text.add(text.substring(i*500, end));
			}
		}
		else {
			this.text.add(text);
		}
	}

	public String getEncodedKey() {
		return KeyFactory.keyToString(key);
	}

	public void setKey(Key key) {
		this.key = key;
	}

	public void setNodeKey(String encodedKey) {
		this.nodeKey = encodedKey;
	}

	public Boolean getBold() {
		return bold;
	}

	public void setBold(Boolean bold) {
		this.bold = bold;
	}

	public Integer getAlignment() {
		return alignment;
	}

	public void setAlignment(Integer alignment) {
		this.alignment = alignment;
	}

	public Integer getFontsize() {
		return fontsize;
	}

	public void setFontsize(Integer fontsize) {
		this.fontsize = fontsize;
	}

	public Text toRemoteObject() {
		Text text = new Text();
		text.setText(getText());
		text.setAlign(alignment);
		text.setBold(bold);
		text.setFontsize(fontsize);

		return text;
	}
	
	
}
